/*
 * Copyright 2009 Donny A. Wijaya
 *  
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */
package com.googlecode.jqplugins.servlet;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.js.resource.ResourceServlet;
import org.springframework.util.StringUtils;
/**
 * WebResourceServlet class.
 * 
 * @author Donny A. Wijaya
 */
public class WebResourceServlet extends ResourceServlet {
	private static final long serialVersionUID = -6288862602004683044L;
	private static final Log log = LogFactory.getLog(WebResourceServlet.class);

	private final String DEVELOPMENT = "development";
	private final String TEST		 = "test";
	private final String PRODUCTION  = "production";
	
	private final String[] DEVELOPMENT_RESOURCE_PATHS = {"META-INF/development/**/*.css", "META-INF/development/**/*.gif",
														 "META-INF/development/**/*.ico", "META-INF/development/**/*.jpeg",
														 "META-INF/development/**/*.jpg", "META-INF/development/**/*.js",
														 "META-INF/development/**/*.png"};

	private final String[] PRODUCTION_RESOURCE_PATHS = {"META-INF/production/**/*.css", "META-INF/production/**/*.gif",
														"META-INF/production/**/*.ico", "META-INF/production/**/*.jpeg",
														"META-INF/production/**/*.jpg", "META-INF/production/**/*.js",
														"META-INF/production/**/*.png"};
	

	private final String[] PUBLIC_RESOURCE_PATHS = {"/**/*.css", "/**/*.gif", "/**/*.ico", "/**/*.jpeg", 
													"/**/*.jpg", "/**/*.js", "/**/*.png",
													"META-INF/**/*.css", "META-INF/**/*.gif", "META-INF/**/*.ico", "META-INF/**/*.jpeg", 
													"META-INF/**/*.jpg", "META-INF/**/*.js", "META-INF/**/*.png"};

	private String getInitialResourcePaths() {
		String publicResourcePaths = StringUtils.arrayToCommaDelimitedString(PUBLIC_RESOURCE_PATHS);
		
		return publicResourcePaths;
	}
	
	private String getDevelopmentResourcePaths() {
		String developmentResourcePaths = StringUtils.arrayToCommaDelimitedString(DEVELOPMENT_RESOURCE_PATHS);
		
		return getInitialResourcePaths().concat(",").concat(developmentResourcePaths);
	}
	
	private String getProductionResourcePaths() {
		String productionResourcePaths = StringUtils.arrayToCommaDelimitedString(PRODUCTION_RESOURCE_PATHS);
		
		return getInitialResourcePaths().concat(",").concat(productionResourcePaths);
	}
	
	public final void setEnvironment(String environment) {
		environment = environment.trim().toLowerCase();
		environment = (environment == null)? DEVELOPMENT : environment;
		
		if (log.isDebugEnabled())
			log.debug("Environment is set to: ".concat(environment));
		
		if (environment.equals(DEVELOPMENT)) {
			setJarPathPrefix("META-INF/development");
			setAllowedResourcePaths(getDevelopmentResourcePaths());
		} else if (environment.equals(PRODUCTION) || environment.equals(TEST)) {
			setJarPathPrefix("META-INF/production");
			setAllowedResourcePaths(getProductionResourcePaths());
		} else
			throw new IllegalArgumentException("Environment value is invalid!! Use development/production/test.");
	}
}